<!-- 搜索主计量单位列表 -->
<template>
  <el-dialog
    :visible.sync="mainUnitVisible"
    title="选择主计量单位"
    width="60%"
    @close="handleCancle"
  >
    <el-row>
      <el-form ref="queryData" :model="queryData" label-width="100px" @submit.native.prevent>
        <el-row type="flex" justify="space-between">
          <el-form-item label="计量单位名称" prop="unitName">
            <el-input
              v-model="queryData.unitName"
              placeholder="请输入计量单位名称"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleQueryData">查询</el-button>
            <el-button @click="handleResetData('queryData')">清空</el-button>
          </el-form-item>
        </el-row>
      </el-form>
    </el-row>
    <el-row class="format_table_wrap">
      <el-table
        v-loading="tableLoading"
        ref="multipleTable"
        border="border"
        use-virtual
        :data="mainUnitTableData"
      >
        <el-table-column
          v-for="column in mainUnitTableColumns"
          :key="column.key"
          :prop="column.key"
          :label="column.title"
          :property="column.key"
          :align="column.align || 'center'"
          :min-width="120"
          :fixed="column.fixed"
        >
          <template v-if="column.slot === 'action'" v-slot="scope">
            <el-button
              type="success"
              size="small"
              @click="handleChooseUnit(scope.row)"
              >选择</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-row>
    <div class="pagination_wrap">
      <el-pagination
        background
        :total="totalPage"
        :current-page="nowPage"
        :page-size="pageShow"
        @current-change="handleChangePage"
        @size-change="handleChangePageSize"
        layout="total, sizes, prev, pager, next, jumper"
      >
      </el-pagination>
      <div slot="footer" style="display: none"></div>
    </div>
  </el-dialog>
</template>
<script>
import { ApiRequest } from '@api/basePage';
export default {
  props: {
    visible: {
      type: Boolean,
      required: true
    }
  },
  data () {
    return {
      mainUnitVisible: this.visible,
      tableLoading: false,
      queryData: {
        unitName: ''
      },
      mainUnitTableData: [],
      mainUnitTableColumns: [
        {
          title: '计量单位名称',
          key: 'unitName'
        },
        {
          title: '默认小数位',
          key: 'decimalPlaces'
        },
        {
          title: '操作',
          slot: 'action',
          fixed: 'right'
        }
      ],
      nowPage: 1, // 当前页
      pageShow: 10, // 每页条数
      totalPage: 0 // 总条数
    };
  },
  watch: {
    visible (newVal) {
      this.mainUnitVisible = newVal;
      newVal && this.getDataListAction();
    }
  },
  methods: {
    /* 获取计量单位列表 */
    getDataListAction () {
      let searchParam = [
        { name: 'unitName', value: this.queryData.unitName },
        { name: 'unitName_q', value: 'Like' }
      ];
      let data = {
        nowPage: this.nowPage,
        pageShow: this.pageShow,
        searchParam: JSON.stringify(searchParam)
      };
      this.tableLoading = true;
      ApiRequest('/sysback/unit/baseQueryList', 'post', data)
        .then((res) => {
          this.tableLoading = false;
          if (res.retStatus !== '1') {
            return false;
          }
          this.mainUnitTableData = res.retData.results;
          this.totalPage = res.retData.totalNum;
          this.mainUnitVisible = true;
        })
        .catch(() => {
          this.tableLoading = false;
        });
    },
    /* 选择计量单位 */
    handleChooseUnit (row) {
      this.mainUnitVisible = false;
      this.$emit('returnMainUnitName', row.unitName);
      this.$emit('returnMainUnit', row);
      this.$emit('update:visible', this.mainUnitVisible);
    },
    /* 查询方法 */
    handleQueryData () {
      this.getDataListAction();
    },
    /* 清空方法 */
    handleResetData (name) {
      this.$refs[name].resetFields();
      this.getDataListAction();
    },
    handleChangePage (page) {
      this.nowPage = page;
      this.getDataListAction();
    },
    handleChangePageSize (size) {
      this.pageShow = size;
      this.getDataListAction();
    },
    handleCancle () {
      this.mainUnitVisible = false;
      this.$emit('update:visible', this.mainUnitVisible);
    }
  }
};
</script>
<style lang="less" scoped>
/deep/ .el-dialog__header {
  border-bottom: 1px solid #e8eaec;
}
/* 分页样式 */
.pagination_wrap {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}
</style>
